//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.bpmn.behavior;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.activiti.bpmn.model.CallActivity;
import org.activiti.bpmn.model.FlowElement;
import org.activiti.bpmn.model.MapExceptionEntry;
import org.activiti.bpmn.model.Process;
import org.activiti.bpmn.model.ValuedDataObject;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.delegate.SubProcessActivityBehavior;
import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManager;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;

public class CallActivityBehavior extends AbstractBpmnActivityBehavior implements SubProcessActivityBehavior {
  private static final long serialVersionUID = 1L;
  protected String processDefinitonKey;
  protected Expression processDefinitionExpression;
  protected List<MapExceptionEntry> mapExceptions;

  public CallActivityBehavior(String processDefinitionKey, List<MapExceptionEntry> mapExceptions) {
    this.processDefinitonKey = processDefinitionKey;
    this.mapExceptions = mapExceptions;
  }

  public CallActivityBehavior(Expression processDefinitionExpression, List<MapExceptionEntry> mapExceptions) {
    this.processDefinitionExpression = processDefinitionExpression;
    this.mapExceptions = mapExceptions;
  }

  public void execute(DelegateExecution execution) {
    String finalProcessDefinitonKey = null;
    if (this.processDefinitionExpression != null) {
      finalProcessDefinitonKey = (String)this.processDefinitionExpression.getValue(execution);
    } else {
      finalProcessDefinitonKey = this.processDefinitonKey;
    }

    ProcessDefinition processDefinition = this.findProcessDefinition(finalProcessDefinitonKey, execution.getTenantId());
    Process subProcess = ProcessDefinitionUtil.getProcess(processDefinition.getId());
    if (subProcess == null) {
      throw new ActivitiException("Cannot start a sub process instance. Process model " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") could not be found");
    } else {
      FlowElement initialFlowElement = subProcess.getInitialFlowElement();
      if (initialFlowElement == null) {
        throw new ActivitiException("No start element found for process definition " + processDefinition.getId());
      } else if (ProcessDefinitionUtil.isProcessDefinitionSuspended(processDefinition.getId())) {
        throw new ActivitiException("Cannot start process instance. Process definition " + processDefinition.getName() + " (id = " + processDefinition.getId() + ") is suspended");
      } else {
        ProcessEngineConfigurationImpl processEngineConfiguration = Context.getProcessEngineConfiguration();
        ExecutionEntityManager executionEntityManager = Context.getCommandContext().getExecutionEntityManager();
        ExpressionManager expressionManager = processEngineConfiguration.getExpressionManager();
        ExecutionEntity executionEntity = (ExecutionEntity)execution;
        CallActivity callActivity = (CallActivity)executionEntity.getCurrentFlowElement();
        String businessKey = null;
        ExecutionEntity subProcessInstance;
        if (!StringUtils.isEmpty(callActivity.getBusinessKey())) {
          Expression expression = expressionManager.createExpression(callActivity.getBusinessKey());
          businessKey = expression.getValue(execution).toString();
        } else if (callActivity.isInheritBusinessKey()) {
          subProcessInstance = (ExecutionEntity)executionEntityManager.findById(execution.getProcessInstanceId());
          businessKey = subProcessInstance.getBusinessKey();
        }

        subProcessInstance = Context.getCommandContext().getExecutionEntityManager().createSubprocessInstance(processDefinition, executionEntity, businessKey);
        Context.getCommandContext().getHistoryManager().recordSubProcessInstanceStart(executionEntity, subProcessInstance, initialFlowElement);
        Map<String, Object> variables = this.processDataObjects(subProcess.getDataObjects());
        Map variablesFromExtensionFile;
        if (callActivity.isInheritVariables()) {
          variablesFromExtensionFile = execution.getVariables();
          Iterator var15 = variablesFromExtensionFile.entrySet().iterator();

          while(var15.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry)var15.next();
            variables.put((String)entry.getKey(), entry.getValue());
          }
        }

        variablesFromExtensionFile = this.calculateInboundVariables(execution, processDefinition);
        variables.putAll(variablesFromExtensionFile);
        if (!variables.isEmpty()) {
          this.initializeVariables(subProcessInstance, variables);
        }

        ExecutionEntity subProcessInitialExecution = executionEntityManager.createChildExecution(subProcessInstance);
        subProcessInitialExecution.setCurrentFlowElement(initialFlowElement);
        Context.getAgenda().planContinueProcessOperation(subProcessInitialExecution);
        Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createProcessStartedEvent(subProcessInitialExecution, variables, false));
      }
    }
  }

  public void completing(DelegateExecution execution, DelegateExecution subProcessInstance) throws Exception {
    Map<String, Object> outboundVariables = this.calculateOutBoundVariables(execution, subProcessInstance.getVariables());
    if (outboundVariables != null) {
      execution.setVariables(outboundVariables);
    }

  }

  public void completed(DelegateExecution execution) throws Exception {
    this.leave(execution);
  }

  protected ProcessDefinition findProcessDefinition(String processDefinitionKey, String tenantId) {
    return tenantId != null && !"".equals(tenantId) ? Context.getProcessEngineConfiguration().getDeploymentManager().findDeployedLatestProcessDefinitionByKeyAndTenantId(processDefinitionKey, tenantId) : Context.getProcessEngineConfiguration().getDeploymentManager().findDeployedLatestProcessDefinitionByKey(processDefinitionKey);
  }

  protected Map<String, Object> processDataObjects(Collection<ValuedDataObject> dataObjects) {
    Map<String, Object> variablesMap = new HashMap();
    if (dataObjects != null) {
      variablesMap = new HashMap(dataObjects.size());
      Iterator var3 = dataObjects.iterator();

      while(var3.hasNext()) {
        ValuedDataObject dataObject = (ValuedDataObject)var3.next();
        variablesMap.put(dataObject.getName(), dataObject.getValue());
      }
    }

    return variablesMap;
  }

  protected void initializeVariables(ExecutionEntity subProcessInstance, Map<String, Object> variables) {
    subProcessInstance.setVariables(variables);
  }

  public void setProcessDefinitonKey(String processDefinitonKey) {
    this.processDefinitonKey = processDefinitonKey;
  }

  public String getProcessDefinitonKey() {
    return this.processDefinitonKey;
  }

  protected Map<String, Object> calculateInboundVariables(DelegateExecution execution, ProcessDefinition processDefinition) {
    return new HashMap();
  }

  protected Map<String, Object> calculateOutBoundVariables(DelegateExecution execution, Map<String, Object> subProcessVariables) {
    return new HashMap();
  }
}
